<?php

require_once('../../connection/_dbconnect_.php');
require_once('m_activity_log.php');

function getPendingRequests(){
		
	$parentArray = array();
	
	$sql = "SELECT scr.idshift_change AS id, 
				   scr.requesting_user AS username, 
				   scr.currentShift AS current_shift_id,
				   ssa.name AS current_shift_name,
				   scr.requestedShift AS requested_shift_id, 
				   ssb.name AS requested_shift_name,
				   scr.day_requested AS requested_day,
				   scr.request_status as status,\n
				   scr.target_user as target_user \n"
    . "FROM shift_change_request scr \n"
    . "JOIN shift_schedule ssa ON ( scr.currentShift = ssa.idshift_schedule ) "
    . "JOIN shift_schedule ssb ON ( scr.requestedShift = ssb.idshift_schedule ) "
    . "ORDER BY scr.day_requested ASC;";
	
	//echo $sql;exit;
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else {
		while($obj = mysql_fetch_object($result)){
		
			if($obj->status == 'Pending'){
				$childArray = array();		
				$childArray[] = $obj->id;
				$childArray[] = $obj->username;
				$childArray[] = $obj->current_shift_id;
				$childArray[] = $obj->current_shift_name;
				$childArray[] = $obj->requested_shift_id;
				$childArray[] = $obj->requested_shift_name;
				$childArray[] = $obj->requested_day;
				$childArray[] = $obj->status;
				$childArray[] = $obj->target_user;
				
				$parentArray[]= $childArray;
			}
		}

		return $parentArray;
	}
}

function getDaySchedule($date){
	
	$parentArray = array();
	
	$refDate = DateTime::createFromFormat('Y-m-d', $date);
	$dateDOW = $refDate->format('l');	
	
	$sql = 	"SELECT  us.iduser AS username,
					 u.fullname,
					 ss.name as shift_name,
					 ss.start_time \n"
			. "FROM  schedule s \n"
			. "JOIN weekday w ON (s.idweekday = w.idweekday)\n"
			. "JOIN user_schedule us ON (s.idschedule = us.idschedule) \n"
			. "JOIN user u ON (us.iduser = u.iduser) \n"
			. "JOIN shift_schedule ss ON (s.idshift_schedule = ss.idshift_schedule) \n"
			. "WHERE  w.name LIKE '".$dateDOW."'\n"
			. "ORDER BY ss.start_time ASC" ;
		
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else {
		while($obj = mysql_fetch_object($result)){
			$childArray = array();	
			$childArray[] = $obj->username;
			$childArray[] = $obj->fullname;
			$childArray[] = $obj->shift_name;
			$childArray[] = $obj->start_time;
			
			$parentArray[]= $childArray;		
		}
		
		return $parentArray;
	}
}

function getDayScheduleStats($date){
	//VOY ACA CON LO STATS DEL DIA
	// INICIANDO EDICION DE COPY&PASTE
	$parentArray = array();
	
	$refDate = DateTime::createFromFormat('Y-m-d', $date);
	$dateDOW = $refDate->format('l');	
	
	$sql = 	"SELECT  us.iduser AS username,
					 u.fullname,
					 ss.name as shift_name,
					 ss.start_time \n"
			. "FROM  schedule s \n"
			. "JOIN weekday w ON (s.idweekday = w.idweekday)\n"
			. "JOIN user_schedule us ON (s.idschedule = us.idschedule) \n"
			. "JOIN user u ON (us.iduser = u.iduser) \n"
			. "JOIN shift_schedule ss ON (s.idshift_schedule = ss.idshift_schedule) \n"
			. "WHERE  w.name LIKE '".$dateDOW."'\n"
			. "ORDER BY ss.start_time ASC" ;
		
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) 
		return false;
	else {
		while($obj = mysql_fetch_object($result)){
			$childArray = array();	
			$childArray[] = $obj->username;
			$childArray[] = $obj->fullname;
			$childArray[] = $obj->shift_name;
			$childArray[] = $obj->start_time;
			
			$parentArray[]= $childArray;		
		}
		
		return $parentArray;
	}
}

function approvePendingRequest($id){
	
	$sql = "update shift_change_request set request_status='Approved' where idshift_change='".$id."';";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Schedule Change Request=".$id." was Approved!");	
		return true;
	}
}

function denyPendingRequest($id){
	
	$sql = "update shift_change_request set request_status='Denied' where idshift_change='".$id."';";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Schedule Change Request=".$id." was denied");	
		return true;
	}
}

?>